// packageA/Calculator/Calculator.js
import {
  areaList
} from '@vant/area-data';
import { carList, getfinancebank, carDetails, getFinanceAll} from "../../api/car.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    $https: getApp().globalData.$https,
    carid:18,
    total:120000,
    radio: '1',
    cellTxt: '选择城市',
    show:false,
    carTxt: '选择车型',
    disabled:false,
    financeList:[
      '智慧租赁',
      '微众银行',
      '狮桥租赁',
      '平安汽融'
    ],
    financeIndex:0,
    proportionIndex:15,
    monthList:[
      '12期',
      '24期',
      '36期',
      '48期',
      '60期',
    ],
    monthIndex:2,
    interestRate:0.38,
    automoList: [{
      values: [],
    },
    {
      values: [],
    },
    ],
    minRatio:15,
  },
  onAutomoCancel(e) {
    this.setData({
      showNew: false,
    });

    var id = this.data.automoList[0].values[this.data.index1].id
    this.carList(id);
  },
  getdetail(id) {
    carDetails(id).then(res => {
      this.setData({
        cardetail: res.data,
        total:res.data.price
      })
    })
  },
  getfinancebank(){
    getfinancebank().then(res => {
      console.log(res)
      this.setData({
        financeList:res.list
      })
    })
  },
  autoPopup() {
  
    this.setData({
      showNew: true,
      "automoList[1].values": this.data.getFinanceAllList[1]
    });
    // const picker = this.selectComponent('#picker') //获取组件实例
    // picker.setColumnIndex(0, this.data.index1)
    // picker.setColumnIndex(1, this.data.index2)
  },
  onAutomoConfirm(e) {
    console.log(e,"onAutomoConfirm")
    var indexarr = e.detail.index;

    var value = e.detail.value;
    console.log(value)
    this.setData({
      index1:indexarr[0],
      index2:indexarr[1],
      categoryName: value[0].name,
      car: value[1] ? value[1].name : '',
      showNew: false,
      carTxt: value[0].name + (value[1] ? value[1].name : ''),
      carid: value[1] ? value[1].id : this.data.carid,
    });
    this.getdetail(this.data.carid)
  },
  financeClick(e){
    let index = e.currentTarget.dataset.index
    this.setData({
      financeIndex:index,
      interestRate:this.data.financeList[index].yearRate,
      minRatio:this.data.financeList[index].minRatio,
      proportionIndex:this.data.financeList[index].minRatio,
    })
  },
  monthClick(e){
    this.setData({
      monthIndex:e.currentTarget.dataset.index
    })
  },
  showPopup() {
    this.setData({
      show: true,
      disabled:true
    });
  },
  sliderChang(e){
    this.setData({
      proportionIndex: e.detail,
    });
  },
  wancheng(e) {
    this.setData({
      show: false,
      disabled:false,
      cellTxt: e.detail.values[0].name + e.detail.values[1].name,
      city: e.detail.values[1].name,
      province: e.detail.values[0].name,
    });
  },
  quxiao(e) {
    this.setData({
      show: false,
      disabled:false
    });
  },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  onChangeCell(e){
    console.log(e.detail.value[0].id)
    this.setData({
      showNew: true,
      "automoList[1].values": this.data.getFinanceAllList[e.detail.value[0].id]
    });
  },
  onDrag(event) {
    this.setData({
      proportionIndex: event.detail.value,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getfinancebank()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    const picker = this.selectComponent('#picker') //获取组件实例
    var index1 = 0;
    var index2 = 0;
    var _this = this;
    carDetails(this.data.carid).then(res => {
      _this.setData({
        cardetail: res.data,

      })
      var id = res.data.carCategoryId
      getFinanceAll(id).then(res => {
        _this.setData({
          getFinanceAllList:res.carlist,
          carTxt:res.category[0].name +''+res.carlist[res.category[0].id][0].name ,
          "automoList[0].values": res.category
        })
      })
    })
  },
  async carList(id) {
    var res = await carList(id)
    this.setData({
      "automoList[0].values": res.category,
      "automoList[1].values": res.list,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})